package praca.impl;

import java.util.Random;

import praca.framework.FPSCounter;
import praca.framework.MusicGL;
import praca.framework.SoundManagerGL;
import praca.framework.TextureGL;
import praca.framework.Vector;
import praca.framework.Vertices;
import praca.interfejsy.Game;
import praca.interfejsy.Music;
import praca.interfejsy.Screen;
import praca.interfejsy.SoundManager;
import praca.interfejsy.Texture;
import praca.interfejsy.TouchEvent;
import android.util.Log;

public class MainMenuScreen extends Screen{

	final static int NR_BRICKS = 100;
	final static int ATLAS_WIDTH = 384;
	final static int ATLAS_HEIGHT = 512;
	final static int PENGUIN_WIDTH = 90;
	final static int PENGUIN_HEIGHT = 2*PENGUIN_WIDTH;
	
	Texture background;
	Texture title;
	Texture startGame;
	Texture help;
	Texture soundOn;
	Texture soundOff;
	Texture menuBrick;
	Texture penguin;
	Texture highScores;
	Music music;
	SoundManager sounds;
	int clickSound;
	FPSCounter counter = new FPSCounter();
	int increment = 0;
	
	Vertices backgroundVert = new Vertices(800, 480, true);
	Vertices startGameVert = new Vertices(500, 70, true);
	Vertices helpVert = new Vertices(500, 70, true);
	Vertices highScoresVert = new Vertices(500, 70, true);
	Vertices titleVert = new Vertices(500, 180, true);
	Vertices soundVert = new Vertices(70, 70, true);
	Vertices brickVert = new Vertices(50, 50, true);
	Vertices penguinVert = new Vertices(0,0,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	Vertices penguinVert2 = new Vertices(128,0,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	Vertices penguinVert3 = new Vertices(256,0,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	
	Vertices revPenguinVert = new Vertices(0,256,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	Vertices revPenguinVert2 = new Vertices(128,256,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	Vertices revPenguinVert3 = new Vertices(256,256,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	
	Vector penguinPosition = new Vector(64,PENGUIN_WIDTH);
	Vector penguinVelocity = new Vector(40, 0);
	Random rand = new Random();
	
	
	Vector gravity = new Vector(0, 0);
	float time;
	boolean isGoingRight = true;
	
	MenuBrick[] bricks;
	private TouchEvent ev;
	
	public MainMenuScreen(Game game) {
		super(game);
		bricks = new MenuBrick[NR_BRICKS];

		
		
		for(int i = 0 ; i < NR_BRICKS; i++){
			bricks[i] = new MenuBrick();
		}
		
		music = new MusicGL(game, "menu.mp3");
		sounds = new SoundManagerGL(game);
		clickSound = sounds.loadSound("click.ogg");
	}

	@Override
	public void update(float deltaTime) {
		
		if(!in.getTouchEvents().isEmpty()){
			ev = in.getTouchEvents().remove(0);
			
			if(ev.getType() == TouchEvent.TOUCH_UP){
				if(checkIfStart(ev.getX(), ev.getY())){
					
					sounds.play(clickSound);
					game.setCurrentScreen(new GameScreen5(game, 1, 0));
				}else if(checkIfSound(ev.getX(), ev.getY())){
					sounds.play(clickSound);
					if(game.isSoundEnabled()){
						if(music.isPlaying())
							music.stop();
						game.setSoundsEnabled(false);
					}
					
					else{
						game.setSoundsEnabled(true);
						if(!music.isPlaying())
							music.play();
					}
				}else if(checkIfHighScores(ev.getX(), ev.getY())){
					sounds.play(clickSound);
					game.setCurrentScreen(new HighScoresScreen(game));
				}else if(checkIfHelp(ev.getX(), ev.getY())){
					sounds.play(clickSound);
					game.setCurrentScreen(new HelpScreen(game));
				}else{
					increment++;
					g.getGl().glViewport(0, 0, game.getView().getWidth()+increment, game.getView().getHeight()+increment);
				}
				
				}//touchup
				in.free(ev);
			
		}//isEmpty
		
		
		
		g.clearTransformations();
		g.moveTexture(400, 240);
		g.drawTexture(background, backgroundVert);
		

		g.setTransparency(0.7f);
		
		for(int i = 0; i<NR_BRICKS; i++){
			g.clearTransformations();
			g.moveTexture(bricks[i].position.x, bricks[i].position.y);
			bricks[i].angle += bricks[i].rotationSpeed*deltaTime;
			g.rotateTexture(bricks[i].angle);
			g.drawTexture(menuBrick, brickVert);
		}
		
		for(int i = 0; i<NR_BRICKS; i++){
			bricks[i].velocity.add(gravity.x *deltaTime, gravity.y*deltaTime);
			bricks[i].position.add(bricks[i].velocity.x * deltaTime, bricks[i].velocity.y * deltaTime);
			if(bricks[i].position.y < -50){
				bricks[i].position.y = 550;
				bricks[i].position.x = rand.nextFloat()*800;
			}
				
			if(bricks[i].angle>360)
				bricks[i].angle = 0;
		}
		
		time = time + deltaTime;
		g.setTransparency(1);
		g.clearTransformations();
		g.moveTexture(penguinPosition.x, penguinPosition.y);
		
		if(isGoingRight){
			if(time<=0.2){
				g.drawTexture(penguin, penguinVert);
			}else if(time<=0.4){
				g.drawTexture(penguin, penguinVert2);
			}else if(time<=0.6){
				g.drawTexture(penguin, penguinVert3);
				
			}else if(time<=0.8){
				g.drawTexture(penguin, penguinVert2);
				
			}else{
				g.drawTexture(penguin, penguinVert2);
				time=0;
			}
		}else{
			if(time<=0.2){
				g.drawTexture(penguin, revPenguinVert);
			}else if(time<=0.4){
				g.drawTexture(penguin, revPenguinVert2);
			}else if(time<=0.6){
				g.drawTexture(penguin, revPenguinVert3);
				
			}else if(time<=0.8){
				g.drawTexture(penguin, revPenguinVert2);
				
			}else{
				g.drawTexture(penguin, revPenguinVert2);
				time=0;
			}
		}
		
		
		
		
		
		penguinPosition.add(penguinVelocity.x * deltaTime,penguinVelocity.y * deltaTime);
		
		if(penguinPosition.x>870){
			isGoingRight = false;
			penguinVelocity.x=-penguinVelocity.x;
		}
		if(penguinPosition.x<-50){
			isGoingRight = true;
			penguinVelocity.x=-penguinVelocity.x;
		}
			
		
		g.clearTransformations();
		g.moveTexture(400, 370);
		g.drawTexture(title, titleVert);
		
		g.clearTransformations();
		g.moveTexture(400, 240);
		g.drawTexture(startGame, startGameVert);
		
		g.clearTransformations();
		g.moveTexture(400, 150);
		g.drawTexture(help, helpVert);
		
		g.clearTransformations();
		g.moveTexture(400, 80);
		g.drawTexture(highScores, highScoresVert);
		
		
		g.clearTransformations();
		g.moveTexture(735, 50);
		if(game.isSoundEnabled())
			g.drawTexture(soundOn, soundVert);
		else
			g.drawTexture(soundOff, soundVert);
		
		counter.logFrame();
	}

	@Override
	public void pause() {
		music.pause();
	}

	@Override
	public void resume() {
		background = new TextureGL(game, "menu_tlo.png");
		title = new TextureGL(game, "title.png");
		startGame = new TextureGL(game, "start.png");
		help = new TextureGL(game, "help.png");
		soundOn = new TextureGL(game, "sound_on.png");
		soundOff = new TextureGL(game, "sound_off.png");
		menuBrick = new TextureGL(game, "snowflake.png");
		penguin = new TextureGL(game, "atlas.png");
		highScores = new TextureGL(game, "highscores.png");
		g.setTextureTransparentBackground();
		music.play();
	}

	@Override
	public void dispose() {
		music.stop();
		
		
	}
	
	private boolean checkIfSound(float x, float y){
		if(x < 770 && x>700 && y>45 && y < 95)
			return true;
					return false;
	}
	
	private boolean checkIfStart(float x, float y){
		if(x < 650 && x>150 && y>205 && y < 275)
			return true;
					return false;
	}

	private boolean checkIfHelp(float x, float y){
		if(x < 650 && x>150 && y>115 && y < 185)
			return true;
					return false;
	}
	
	private boolean checkIfHighScores(float x, float y){
		if(x < 650 && x>150 && y>45 && y < 115)
			return true;
					return false;
	}

}
